<template>
  <div id="login" style="text-align: center">
    <el-form
      :model="loginForm"
      status-icon
      :rules="rules"
      ref="loginForm"
      label-width="100px"
      class="demo-ruleForm"
      style="width: 30%;display: inline-block"
    >
      <el-form-item>
        <h1>登录</h1>
      </el-form-item>

      <el-form-item label prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>

      <el-form-item label prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
import { getUserInfo, Login, getExportInfo } from '@/network/index'

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      token: ''
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let data = {
            username: this.loginForm.username,
            password: this.loginForm.password
          }
          Login(data)
            .then(res => {
              if (res.code == 200) {
                this.$message({
                  showClose: true,
                  message: res.message,
                  type: 'success'
                })
                this.$store.dispatch('SignIn')
                this.$store.dispatch('getToken', res.data.token)
                this.$store.dispatch('getUserid', res.data.user_id)
                let params = { user_id: res.data.user_id }
                getUserInfo(params)
                  .then(res => {
                    this.$store.dispatch('getUserInfo', res.data)
                  })
                  .catch(err => {
                    return console.log(err)
                  })
                getExportInfo(params)
                  .then(res => {
                    this.$store.dispatch('getExpertinfo', res.data)
                  })
                  .catch(err => {
                    this.$toast(err.data.message, 'error')
                  })

                this.$router.push('/home')
              } else {
                this.$message({
                  showClose: true,
                  message: res.message,
                  type: 'error'
                })
              }
            })
            .catch(err => {
              console.log('失败')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
#login {
  position: fixed;
  z-index: 2000;
  bottom: 0;
  top: 0;
  left: 0;
  right: 0;
  background-position: center;
  background-size: cover;
  background-image: url('http://qiniu.careerai.com.cn/FtjX_xMPzQW7mmOypli29QwCAAOx');
}
</style>
